﻿using Autofac;
using leapt.Repositories;
using leapt.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace leapt.Api
{
    public class AutofacModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            //注册仓储服务
            //builder.RegisterType<UserRepository>().As<IUserRepository>().InstancePerLifetimeScope().PropertiesAutowired();
            //注册services层服务
            //builder.RegisterType<UserService>().As<IUserService>().InstancePerLifetimeScope().PropertiesAutowired();

            var baseType = typeof(IDependency);
            builder.RegisterAssemblyTypes(typeof(IRepository<>).Assembly)
                            .Where(t => baseType.IsAssignableFrom(t) && !t.IsAbstract)
                            .AsSelf()
                            .AsImplementedInterfaces()
                            .InstancePerLifetimeScope()
                            .PropertiesAutowired();

            builder.RegisterAssemblyTypes(typeof(IService<>).Assembly)
                            .Where(t => baseType.IsAssignableFrom(t) && !t.IsAbstract)
                            .AsSelf()
                            .AsImplementedInterfaces()
                            .InstancePerLifetimeScope()
                            .PropertiesAutowired();

            //作为属性注入控制器
            // 在web api中暂时属性注入出错
            //builder.RegisterAssemblyTypes(typeof(Controllers.BaseController).Assembly).InstancePerLifetimeScope().PropertiesAutowired();

        }
    }
}
